package com.flyqiu.common.transaction;

import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import javax.sql.DataSource;

public class SimpleTransactionManage implements FlyQiuTransactionManage {


    private final PlatformTransactionManager platformTransactionManager;

    public SimpleTransactionManage() {
        this(new JdbcTransactionManager());
    }

    public SimpleTransactionManage(DataSource dataSource) {
        this(new JdbcTransactionManager(dataSource));
    }


    public SimpleTransactionManage(PlatformTransactionManager platformTransactionManager) {
        this.platformTransactionManager = platformTransactionManager;
    }


    @Override
    public TransactionStatus begin(TransactionDefinition transactionDefinition) {
        return platformTransactionManager.getTransaction(transactionDefinition);
    }

    @Override
    public void commit(TransactionStatus transactionStatus) {
        platformTransactionManager.commit(transactionStatus);
    }

    @Override
    public void rollback(TransactionStatus transactionStatus) {
        platformTransactionManager.rollback(transactionStatus);
    }
}
